from flask import Flask,request,jsonify
import pandas as pd
from itertools import combinations

app = Flask(__name__)

frequent_itemsets = pd.read_pickle('./frequent_itemsets.pk1')
rules = pd.read_pickle('./rule.pk1')

@app.route("/recommend",method=['POST'])
def recommend():
    data = request.json.get('item',[])

    recommendDations = []
    for idx ,rule in rules.iterrows():
        antecedents = list(rule['antecedents'])
        consequents = list(rule['consequents'])

        if set(antecedents).issubset(set(data)):
            recommendDations.extend(consequents)
    
    recommendDations = list(set(recommendDations)-set(data))
    return jsonify({"recommendDations":recommendDations})

    

if __name__=="__main__":
    app.run()